package com.cj.controller;

import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
public class AjaxController {
    private ArrayList<String> list = new ArrayList<>();

    @GetMapping("/ajaxGet")
    @CrossOrigin    //让url支持跨域访问
    public String doAjaxGet(){
        try {
            Thread.sleep(1000*5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "返回的Ajax";
    }

    @GetMapping("/ajaxName")
    public String doName(String name){
        if(list.contains(name)){
            return name + "/已存在";
        }else{
            return name + "/可以注册";
        }
   }

   @PostMapping("/doSave")
    public String doInsert(String name){
        String[] values = this.doName(name).split("\\/");
       System.out.println();
        if(values[values.length - 1].equals("已存在")) {
            return "用户名已被注册，请重试";
        }
        else{
            list.add(name);
            return "注册成功啦";
        }
   }
}
